package com.foredu.fiqs.web.action;

import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.foredu.fiqs.domain.Department;
import com.foredu.fiqs.domain.Employee;
import com.foredu.fiqs.domain.Salary;
import com.foredu.fiqs.domain.SalaryItemType;
import com.foredu.fiqs.service.IDataImportService;
import com.foredu.fiqs.service.IManagementService;
import com.foredu.fiqs.web.actionform.DataImportActionForm;
import com.foredu.fiqs.web.util.ReadExcelUtil;

/**
 * Description:<br/>
 * 处理财务数据导入请求，负责上传并解析Excel格式的财务数据<br/>
 * Date:<br/>
 * 2014/5/16<br/>
 * 
 * @author hushigang
 */

public class DataImportAction extends AbstractBaseDispatchAction {

	private IDataImportService dataImportService;
	private IManagementService managementService;
	
	// 跳转到财务数据导入页面
	public ActionForward toImport(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		//查询所有工资条目类型，并显示到页面
		List<SalaryItemType> types = dataImportService.findSalaryItemTypes();
		request.setAttribute("types", types);
		
		//查询所有部门，并显示到页面
		List<Department> depts = managementService.findDepartments();
		request.setAttribute("depts", depts);
		
		return mapping.findForward("toImport");
	}

	// 向数据库导入数据
	public ActionForward importData(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		DataImportActionForm dif = (DataImportActionForm) form;

		// 获得工资年月
		int year = dif.getYear();
		int month = dif.getMonth();

		// 获得工资报表所在的部门
		int deptId = dif.getDeptId();
		Department dept = managementService.findDepartmentById(deptId);
		
		// 获得用户上传工资条目类型
		int salaryItemType = dif.getSalaryItemType();
		SalaryItemType siType = dataImportService.findSalaryItemTypeById(salaryItemType);

		// 获得用户上传的EXCEL报表
		FormFile formFile = dif.getFile();
		if (formFile != null) {
			System.out.println("fileName=" + formFile.getFileName());
			InputStream is = formFile.getInputStream();
			List<Salary> salaryList = ReadExcelUtil.read(is, siType, year, month);
			if (salaryList != null && salaryList.size() > 0) {
				for (Salary salary : salaryList) {
					//通过教职工编号查找教职工对象
					Employee employee = dataImportService.findEmployeeByEmpId(salary.getEmployee().getEmpId());
					salary.setEmployee(employee);
					dataImportService.saveSalary(salary);
				}
			}
		}
		return mapping.findForward("importDataSuccess");
	}

	public void setDataImportService(IDataImportService dataImportService) {
		this.dataImportService = dataImportService;
	}

	public IDataImportService getDataImportService() {
		return dataImportService;
	}

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}
}
